package cn.hengzhu.main_manager.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hengzhu.main_manager.repository.domain.*;
import cn.hengzhu.main_manager.utils.ReturnMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hengzhu.main_manager.repository.dao.ManageCaseBoxMapper;
import cn.hengzhu.main_manager.repository.dao.PayOrderMapper;
import cn.hengzhu.main_manager.repository.dao.PrepayOrderMapper;
import cn.hengzhu.main_manager.service.PayOrderService;
import cn.hengzhu.main_manager.utils.Md5Util;

@Service
@Transactional(rollbackFor = Exception.class)
public class PayOrderServiceImpl implements PayOrderService {

	@Autowired
	private PayOrderMapper payOrderMapper;
	@Autowired
	private PrepayOrderMapper prepayOrderMapper;
	@Autowired
	private ManageCaseBoxMapper manageCaseBoxMapper;

	/**
	 * @author liuCheng
	 * @since 2018年12月12日 下午2:27:46
	 * @param date
	 * @param openId
	 * @param out_trade_no
	 * @param amount
	 * @param boxId
	 */
	@Override
	public void prepayWXBefore(Date date, String openId, String out_trade_no, Integer amount, BigDecimal pledge,
			Integer boxId, String prepay_id, String password) {

		// 在数据库中插入订单记录
		PayOrder record = new PayOrder();
		record.setCreartTime(date);
		record.setOpenId(openId);
		record.setOutTradeNo(out_trade_no);
		record.setTotalFee(amount);
		record.setBoxId(boxId);
		record.setPrepayId(prepay_id);
		record.setPayPledge(pledge);// 押金
		if (password != null && !"".equals(password)) {
			record.setPassword(Md5Util.sha1(password));
		}
		payOrderMapper.insertSelective(record);

		// 预付订单生成后还需要，在预付订单中插入一条数据
		PrepayOrder prepayOrderRecord = new PrepayOrder();
		prepayOrderRecord.setBoxId(boxId);
		prepayOrderRecord.setCreartTime(date);
		prepayOrderRecord.setOpenId(openId);
		prepayOrderRecord.setOutTradeNo(out_trade_no);
		prepayOrderMapper.insertSelective(prepayOrderRecord);

	}

	/**
	 * @author liuCheng
	 * @since 2018年12月12日 下午2:27:46
	 * @param date
	 * @param openId
	 * @param out_trade_no
	 * @param amount
	 * @param boxId
	 */
	public void prepayWXBefore(Date date, String openId, String out_trade_no, Integer amount, BigDecimal pledge,
							   Integer boxId, String prepay_id) {

		// 在数据库中插入订单记录
		PayOrder record = new PayOrder();
		record.setCreartTime(date);
		record.setOpenId(openId);
		record.setOutTradeNo(out_trade_no);
		record.setTotalFee(amount);
		record.setBoxId(boxId);
		record.setPrepayId(prepay_id);
		record.setPayPledge(pledge);// 押金

		payOrderMapper.insertSelective(record);

	}


	@Override
	public void timeOutPrepayOrder(List<Integer> prepayOrderIds, List<Integer> boxIds) {
		// 1.删除所有
		PrepayOrderExample deletePrepayOrderExample = new PrepayOrderExample();
		deletePrepayOrderExample.createCriteria().andIdIn(prepayOrderIds);
		prepayOrderMapper.deleteByExample(deletePrepayOrderExample);
		// 2.修改储物柜的状态
		ManageCaseBox manageCaseBox = new ManageCaseBox();
		manageCaseBox.setUseStatus((byte) 0);
		ManageCaseBoxExample manageCaseBoxExample = new ManageCaseBoxExample();
		manageCaseBoxExample.createCriteria().andIdIn(boxIds).andUserIdEqualTo("123").andUseStatusEqualTo((byte) 1)
				.andIsDelEqualTo((byte) 0);
		manageCaseBoxMapper.updateByExampleSelective(manageCaseBox, manageCaseBoxExample);

	}

	@Override
	public void prepayAliBefore(String openId, Double amount, String out_trade_no, String tradeNo, Integer boxId) {
		// TODO Auto-generated method stub
		PayOrder record = new PayOrder();
		record.setCreartTime(new Date());
		record.setIsPay((byte) 0);
		record.setOpenId(openId);
		record.setOutTradeNo(out_trade_no);
		Double money = amount * 100;
		record.setTotalFee(money.intValue());
		record.setTransactionId(tradeNo);
		record.setBoxId(boxId);
		payOrderMapper.insertSelective(record);
		// 插入预支付订单
		PrepayOrder prepayOrderRecord = new PrepayOrder();
		prepayOrderRecord.setBoxId(boxId);
		prepayOrderRecord.setCreartTime(new Date());
		prepayOrderRecord.setOpenId(openId);
		prepayOrderRecord.setOutTradeNo(out_trade_no);
		prepayOrderMapper.insertSelective(prepayOrderRecord);

	}

	@Override
	public void prepayAliBefore(String openId, Double amount, BigDecimal cashPledge, String out_trade_no,
			String tradeNo, Integer boxId, String password) {
		// TODO Auto-generated method stub
		PayOrder record = new PayOrder();
		record.setCreartTime(new Date());
		record.setIsPay((byte) 0);
		record.setOpenId(openId);
		record.setOutTradeNo(out_trade_no);
		Double money = amount * 100;
		record.setTotalFee(money.intValue());
		record.setPayPledge(cashPledge);
		record.setTransactionId(tradeNo);
		record.setBoxId(boxId);
		if (password != null && !"".equals(password)) {
			record.setPassword(Md5Util.sha1(password));
		}
		payOrderMapper.insertSelective(record);
		// 插入预支付订单
		PrepayOrder prepayOrderRecord = new PrepayOrder();
		prepayOrderRecord.setBoxId(boxId);
		prepayOrderRecord.setCreartTime(new Date());
		prepayOrderRecord.setOpenId(openId);
		prepayOrderRecord.setOutTradeNo(out_trade_no);
		prepayOrderMapper.insertSelective(prepayOrderRecord);
	}

	@Override
	public void cancelPrepay(PrepayOrder prepayOrder) {
		Integer orderId = prepayOrder.getId();
		Integer boxId = prepayOrder.getBoxId();
		// 删除预支付订单
		prepayOrderMapper.deleteByPrimaryKey(orderId);
		// 修改储物柜状态为空闲
		ManageCaseBox manageCaseBox = manageCaseBoxMapper.selectByPrimaryKey(boxId);
		Integer version = manageCaseBox.getVersion();
		ManageCaseBox record = new ManageCaseBox();
		record.setUseStatus((byte) 0);
		record.setVersion(version + 1);
		ManageCaseBoxExample updateManageCaseBoxExample = new ManageCaseBoxExample();
		updateManageCaseBoxExample.createCriteria().andVersionEqualTo(version).andIdEqualTo(boxId)
				.andUserIdEqualTo("123").andUseStatusEqualTo((byte) 1);
		manageCaseBoxMapper.updateByExampleSelective(record, updateManageCaseBoxExample);

	}

	@Override
	public void creatPayWx(String openId, String out_trade_no, Integer amount, Integer boxId, String prepay_id) {

		PayOrder record = new PayOrder();
		record.setCreartTime(new Date());
		record.setOpenId(openId);
		record.setOutTradeNo(out_trade_no);
		record.setTotalFee(amount);
		record.setBoxId(boxId);
		record.setPrepayId(prepay_id);
		// 在数据库中插入订单记录
		payOrderMapper.insertSelective(record); // insert into pay_order

	}



}
